import { Component, OnInit, OnDestroy } from '@angular/core';
import { NzBreadCrumbModule } from 'ng-zorro-antd/breadcrumb';
import { NzIconModule } from 'ng-zorro-antd/icon';
import { NzLayoutModule } from 'ng-zorro-antd/layout';
import { NzMenuModule } from 'ng-zorro-antd/menu';
import { RouterOutlet } from '@angular/router';
import { RouterLink } from '@angular/router'; 
import { Router, NavigationEnd, Event as RouterEvent } from '@angular/router';
import { Subscription } from 'rxjs';
import { NzBackTopModule } from 'ng-zorro-antd/back-top';
@Component({
  selector: 'app-root',
  standalone: true,
  imports: [
    NzBreadCrumbModule, 
    NzIconModule,
    NzMenuModule, 
    NzLayoutModule,
    RouterOutlet,
    RouterLink,
    NzBackTopModule,
  ],
  templateUrl: './app.component.html',
  styleUrl:'./app.component.css',
      
})
export class AppComponent implements OnInit, OnDestroy {
  isCollapsed = false;
  breadcrumbItems=[
    {path:'',name:'用户管理'},
    {path:'/list1',name:'列表1'},
  ]
  private routerEventsSubscription: Subscription | undefined;
 
  constructor(private router: Router) {}
 
  ngOnInit() {
    this.routerEventsSubscription = this.router.events.subscribe((event: RouterEvent) => {
      if (event instanceof NavigationEnd) {
        console.log('this.router', this.router)
        console.log('路由变化后的URL:', event.url);
        // 在这里处理路由变化后的逻辑
        let breadcrumbItems: { path: string; name: any; }[] = []
        this.router.config.forEach(item=>{
          let path =item?.path
          let name =item?.data?.['name']

          let parentPath =item?.data?.['parentPath']
          let parentName =item?.data?.['parentName']
          if ('/' + path=== event.url) {
            breadcrumbItems.push({
              path: parentPath,
              name:parentName
            })
            breadcrumbItems.push({
              path:event.url,
              name:name
            })
          }
        })
        this.breadcrumbItems = breadcrumbItems
      }
    });
  }
 
  ngOnDestroy() {
    if (this.routerEventsSubscription) {
      this.routerEventsSubscription.unsubscribe();
    }
  }
}